<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>数据</el-breadcrumb-item>
        <el-breadcrumb-item>产品</el-breadcrumb-item>
        <el-breadcrumb-item>新增留存率</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        :key="indexOde"
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="form = {}"
        @search-change="searchChange"
      >
      <template slot="date_2" slot-scope="scope">
        {{ scope.row.retentionInfo ? resultObj(scope.row.retentionInfo,2) : '0.00%' }}
      </template>
      <div v-for="(item, index) in newArr" :key="index" :slot="item.prop" slot-scope="scope">
        {{ scope.row.retentionInfo ? resultObj(scope.row.retentionInfo,index+3) : '0.00%' }}
      </div>
      <!-- 自定义按钮 -->
      <template slot-scope="" slot="menuLeft">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="small"
          plain
          @click="handleExcel()"
          >导出Excel</el-button
        >
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { userNewAndRetained as baseURL, selectVersion } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
      },
      indexOde: "",
      newArr: [],
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
          type: 1,
      },
      option: {
        addBtn: false,
        editBtn: false,
        viewBtn: false,
        delBtn: false,
        excelBtn: false,
        menu: false,
        border: true,
        column: [
          {
            label: "日期",
            prop: "statistics_date",
            type: "daterange",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            search: true,
            fixed: "left",
          },
          {
            label: "渠道",
            prop: "channel_id",
            type: "input",
            fixed: "left",
            search: true,
          },
          {
            label: "日活",
            prop: "dailyActiveNum",
            type: "input",
            fixed: "left",
          },
          {
            label: "新增",
            prop: "userIdsNum",
            type: "input",
            fixed: "left",
          },
          {
            label: "2日留存率",
            prop: "date_2",
            type: "input",
            slot: true,
          },
        ],
      },
    };
  },

  methods: {
    //数据导出
  async handleExcel() {
      this.loading = true;
      //处理搜索条件
      let params = { ...this.$refs.crud.searchForm };
      params.route = "数据/产品/新增留存率";

      let export_condition = "";
      let userInfo = this.$store.state.user.userInfo;
      params.export_user_id = userInfo.id;
      params.export_user_name = userInfo.username;
      params.pageNum = 1;
      params.pageSize = 1000; 
      console.log(params)
      if(params.statistics_date.length>0){
        export_condition += " &时间区间："+params.statistics_date[0].toString()+"~"+params.statistics_date[1].toString()
        params.startTime = params.statistics_date[0];
        params.endTime = params.statistics_date[1];
      }
      if(params.channel_id){
        export_condition += " &渠道："+params.channel_id;
      }

      params.export_condition = export_condition == "" ? "所有" : export_condition;

     //调用导出接口
      let response = await common(
        baseURL.exportUserNewAndRetained,
        { ...params, ...this.default },
        null,
        "POST"
      );
      this.loading = false;
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.userNewAndRetained,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      response.data.forEach((element, index) => {
        if(!element.hasOwnProperty('retentionInfo') || element.retentionInfo.length == 0){
          element.retentionInfo = [
              {
                id: element.id,
                n_column: 2,
                retention: "0",
                user_id: []
              }
          ]
        }
    });
    if(this.newArr.length == 0) {
      for (let i = 0; i < 58; i++) {
        let ele = {
          label:  (i + 3) + "日留存率",
          prop: "date_" + (i + 3),
          type: "input",
          slot: true,
        };
        this.newArr.push(ele);
      }
      this.indexOde = 1;
      this.option.column = [...this.option.column, ...this.newArr];
    }
      this.data = response.data;
      this.page = response.page;
      this.loading = false;
    },
    // 转化百分比小数
    resultObj(arr,column) {
      if(arr.length > 0) {
        let result = arr.find ((i) => i.n_column ==  column);
        if(result) {
          return (result.retention * 100).toFixed(2) + "%";
        } else {
          return "0.00%"
        }
      } else {
        return "0.00%"
      }
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      if(params.statistics_date){
        params.startTime = params.statistics_date[0]
        params.endTime = params.statistics_date[1]
      }
 
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
};
</script>